package com.mia.craftstudio.minecraft.client;

import com.mia.craftstudio.libgdx.Vector3;
import com.mia.craftstudio.minecraft.CraftStudioRendererVBO;
import com.mia.craftstudio.minecraft.INodeProvider;
import com.mia.craftstudio.minecraft.ModelMetadata;
import com.mia.craftstudio.minecraft.forge.CSLibMod;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class CSClientModelWrapperVariableVBO extends CSClientModelWrapperVBO {
   private Set staticNodes1st;
   private Set staticNodes2nd;
   private List staticFilters = new ArrayList();
   private List inventoryFilters = new ArrayList();
   private Map roMain = new HashMap();
   Set renderers1st = new HashSet();
   Set renderers2nd = new HashSet();

   public CSClientModelWrapperVariableVBO(ModelMetadata metadata) {
      super(metadata);
      String[] var2 = ((String)metadata.tileParams.get("VarRend_STATIC")).split(";");
      int var3 = var2.length;

      int var4;
      String s;
      String pattern;
      for(var4 = 0; var4 < var3; ++var4) {
         s = var2[var4];
         pattern = s.replace(".", "\\.").replace("*", ".*").replace("?", ".");
         this.staticFilters.add(Pattern.compile(pattern));
      }

      var2 = ((String)metadata.tileParams.get("VarRend_INVENTORY")).split(";");
      var3 = var2.length;

      for(var4 = 0; var4 < var3; ++var4) {
         s = var2[var4];
         pattern = s.replace(".", "\\.").replace("*", ".*").replace("?", ".");
         this.inventoryFilters.add(Pattern.compile(pattern));
      }

   }

   private Set computeNodesSet(Set nodes, List filter) {
      Set outSet = new HashSet();
      Iterator var4 = nodes.iterator();

      while(var4.hasNext()) {
         CraftStudioRendererVBO node = (CraftStudioRendererVBO)var4.next();
         Iterator var6 = filter.iterator();

         while(var6.hasNext()) {
            Pattern p = (Pattern)var6.next();
            String fullName = node.getNode().getFullName();
            if (p.matcher(fullName).matches()) {
               outSet.add(node);
            }
         }
      }

      return outSet;
   }

   protected void precompute() {
      Iterator var1 = this.topRenderers.iterator();

      while(var1.hasNext()) {
         CraftStudioRendererVBO renderer = (CraftStudioRendererVBO)var1.next();
         renderer.collectRenderers(this.renderers1st, this.renderers2nd);
      }

      this.staticNodes1st = this.computeNodesSet(this.renderers1st, this.staticFilters);
      this.staticNodes2nd = this.computeNodesSet(this.renderers2nd, this.staticFilters);
      Set inventoryNodes1st = this.computeNodesSet(this.renderers1st, this.inventoryFilters);
      Set inventoryNodes2nd = this.computeNodesSet(this.renderers2nd, this.inventoryFilters);
      inventoryNodes1st.addAll(this.staticNodes1st);
      inventoryNodes2nd.addAll(this.staticNodes2nd);
      this.roDefault = new CSClientModelWrapperVBO.RenderingObject(this.generateVBOs(inventoryNodes1st, inventoryNodes2nd), new int[]{inventoryNodes1st.size(), inventoryNodes2nd.size()});
      this.isPrecomputed = true;
   }

   public void render(Object instanceRenderState, float timeSinceLastRender, int pass, boolean renderOutline, float rotationAngle, Vector3 rotationVector, Vector3 preRotataionTranslation, Vector3 postRotataionTranslation) {
      if (!this.isPrecomputed) {
         this.precompute();
      }
      GL11.glEnable(GL12.GL_RESCALE_NORMAL);

      GL11.glPushMatrix();
      if (!Vector3.Zero.equals(preRotataionTranslation)) {
         GL11.glTranslatef(preRotataionTranslation.x, preRotataionTranslation.y, preRotataionTranslation.z);
      }

      if (!Vector3.Zero.equals(rotationVector)) {
         GL11.glRotatef(rotationAngle, rotationVector.x, rotationVector.y, rotationVector.z);
      }

      if (!Vector3.Zero.equals(postRotataionTranslation)) {
         GL11.glTranslatef(postRotataionTranslation.x, postRotataionTranslation.y, postRotataionTranslation.z);
      }

      if (instanceRenderState instanceof INodeProvider) {
         INodeProvider tile = (INodeProvider)instanceRenderState;
         String hash = tile.getStatusHash();
         if (hash == null) {
            return;
         }

         if (!this.roMain.containsKey(hash)) {
            Set nodes1st = tile.getNodes(this.renderers1st);
            Set nodes2nd = tile.getNodes(this.renderers2nd);
            nodes1st.addAll(this.staticNodes1st);
            nodes2nd.addAll(this.staticNodes2nd);
            this.roMain.put(hash, new CSClientModelWrapperVBO.RenderingObject(this.generateVBOs(nodes1st, nodes2nd), new int[]{nodes1st.size(), nodes2nd.size()}));
         }

         this.renderWithVBO((CSClientModelWrapperVBO.RenderingObject)this.roMain.get(hash), instanceRenderState, timeSinceLastRender, pass);
         if (renderOutline && CSLibMod.displayOutline) {
            this.renderOutline((CSClientModelWrapperVBO.RenderingObject)this.roMain.get(hash), instanceRenderState, timeSinceLastRender);
         }
      } else {
         this.renderWithVBO(this.roDefault, instanceRenderState, timeSinceLastRender, pass);
      }

      GL11.glPopMatrix();
   }
}
